package project.action;

import org.json.JSONException;
import org.json.JSONObject;
import project.Dao.TaskDao;
import project.Dao.UserDao;
import project.Dao.groupDao;
import project.Domain.GroupClass;
import project.Domain.ID_Name;
import project.Domain.Task;
import project.Domain.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;

public class action1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doGet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doPost");
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("执行了service");
        try {
            processAEdFile(request, response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    protected void processAEdFile(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {
        request.setCharacterEncoding("UTF-8");
//        获取操作方式增删改
        JSONObject json = new JSONObject();
        String action = request.getParameter("action");

        out.println("收到的指令:"+action);
        /////////////获取项目组各成员的进度信息
        if(action.equals("get_member_progress")){
            String user_id = request.getParameter("user_id");
            TaskDao taskDao = new TaskDao();
            List<Task> list = taskDao.getMemberTaskList(Integer.parseInt(user_id));
            List jsonList = new ArrayList();
            for(int i = 0; i < list.size(); i ++){
                Map map = new HashMap();
                map.put("id", list.get(i).getId());
                map.put("leaderID",list.get(i).getLeaderID());
                map.put("leaderName",list.get(i).getLeaderName());
                map.put("toWho",list.get(i).getToWho());
                map.put("toWhoName",list.get(i).getToWhoName());
                map.put("content",list.get(i).getContent());
                map.put("workload",list.get(i).getWorkload());
                map.put("status",list.get(i).getStatus());
                map.put("progress",list.get(i).getProgress());
                map.put("startTime",list.get(i).getStartTime());
                map.put("dueTime",list.get(i).getDueTime());
                jsonList.add(map);
            }
            json.put("result", 1);
            json.put("aaData", jsonList);
        }
        /////////////添加新的任务
        else if(action.equals("add_task")){
            Integer id = 0;
            String leaderID = request.getParameter("leaderID");
            String leaderName = request.getParameter("leaderName");
            String toWho = request.getParameter("toWho");
            String toWhoName = request.getParameter("toWhoName");
            String content = request.getParameter("content");
            String workload = request.getParameter("workload");
            String status = request.getParameter("status");
            String progress = request.getParameter("progress");
            String startTime = request.getParameter("startTime");
            String dueTime = request.getParameter("dueTime");

            Task task = new Task(id,Integer.parseInt(leaderID),leaderName,toWho,toWhoName,content,workload,status,Integer.parseInt(progress),startTime,dueTime);
            TaskDao taskDao = new TaskDao();
            if(taskDao.add(task)){
                json.put("result", 1);
            }
            else json.put("result", 0);
        }
        else if(action.equals("get_task_info")){
            String id = request.getParameter("user_id");
            TaskDao taskDao = new TaskDao();
            List<Task> list = taskDao.getMyTaskList(Integer.parseInt(id));
            List jsonList = new ArrayList();
            for(int i = 0; i < list.size(); i ++){
                Map map = new HashMap();
                map.put("id", list.get(i).getId());
                map.put("leaderID",list.get(i).getLeaderID());
                map.put("leaderName",list.get(i).getLeaderName());
                map.put("toWho",list.get(i).getToWho());
                map.put("toWhoName",list.get(i).getToWhoName());
                map.put("content",list.get(i).getContent());
                map.put("workload",list.get(i).getWorkload());
                map.put("status",list.get(i).getStatus());
                map.put("progress",list.get(i).getProgress());
                map.put("startTime",list.get(i).getStartTime());
                map.put("dueTime",list.get(i).getDueTime());
                jsonList.add(map);
            }
            json.put("result", 1);
            json.put("aaData", jsonList);
        }
        ////////更新项目进度
        else if(action.equals("update_project_progress")){
            String progress = request.getParameter("progress");
            String leaderID = request.getParameter("leaderID");
            groupDao group_Dao = new groupDao();
            if(group_Dao.updateProgress(Integer.parseInt(progress), Integer.parseInt(leaderID))){
                json.put("result", 1);
            }else json.put("result", 0);
        }
        ////////更新任务进度
        else if(action.equals("update_task_progress")){
            String progress = request.getParameter("progress");
            String id = request.getParameter("id");
            String status = progress.equals("100")?"done":"todo";
            TaskDao taskDao = new TaskDao();
            if(taskDao.updateProgress(Integer.parseInt(progress),status, Integer.parseInt(id))){
                json.put("result", 1);
            }else json.put("result", 0);
        }
        ////////////撤销任务
        else if(action.equals("delete_task")){
            String id = request.getParameter("id");
            TaskDao taskDao = new TaskDao();
            if (taskDao.delete(Integer.parseInt(id))){
                json.put("result", 1);
            }else json.put("result", 0);
        }
        //将响应消息返回页面
        out.println("最后构造得到的json是:"+json);
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().print(json);
        response.getWriter().flush();
        response.getWriter().close();
    }
}